﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManageSystem
{
    public partial class form1: Form
    {
        public form1()
        {
            InitializeComponent();
        }
        public static int id;
        public static string name;

        //管理员登陆的方法
        private void AdminLogin()
        {
            //获取账号和密码，去对数据库对应的表里面对比
            int id = int.Parse(txtID.Text);
            string pwd = txtPassword.Text;
            
            Dao dao = new Dao();
            dao.Concnect();
            string sql = $"select * from T_Admin where AdminID = '{id}' and Pwd = '{pwd}'";
            SqlDataReader reader = dao.read(sql);
            if (reader.Read() == true)//如果表里面有该用户的信息
            {
                form1.id = id;
                sql = $"select [Name] from T_Admin where AdminID = '{id}'";
                reader = dao.read(sql);
                reader.Read();
                form1.name = reader[0].ToString();
                txtID.Text = txtPassword.Text = "";
                //登录成功
                FormAdmin formAdmin = new FormAdmin();
                formAdmin.ShowDialog();
            }
            else 
            {
                MessageBox.Show("登陆失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            reader.Close();
            dao.DaoClose();
        }
        //用户登录校验
        private void UserLogin()
        {
            //获取账号和密码，去对数据库对应的表里面对比
            int id = int.Parse(txtID.Text);
            string pwd = txtPassword.Text;

            Dao dao = new Dao();
            dao.Concnect();
            string sql = $"select * from T_User where Uid = '{id}' and Pwd = '{pwd}'";
            SqlDataReader reader = dao.read(sql);
            if (reader.Read() == true)//如果表里面有该用户的信息
            {
                form1.id = id;
                sql = $"select [Uname] from T_User where Uid  = '{id}'";
                reader = dao.read(sql);
                reader.Read();
                form1.name = reader[0].ToString();
                txtID.Text = txtPassword.Text = "";
                //登录成功
                FormUser formUser = new FormUser();
                formUser.ShowDialog();
            }
            else
            {
                MessageBox.Show("登陆失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            reader.Close();
            dao.DaoClose();
        }

        private void 图书管理系统_Load(object sender, EventArgs e)
        {

        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            FormLogon form = new FormLogon();
            form.ShowDialog();
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("确定退出？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
                ==DialogResult.Yes)
                this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //判断文本框是否有内容
            if (txtID.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("请输入账户密码", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //判断登录的对象
            if (rbtnAdmin.Checked == true)
            {
                AdminLogin();
            }
            else if (rbtnUser.Checked == true)
            {
               UserLogin();
            }
            
        }
    }
}
